package com.tma.hellomap;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class HelloMapActivity extends MapActivity implements LocationListener {

	private LocationManager mLocationManager;
	private MapView mMapView;
	private HelloItemizedOverlay mItemizedoverlay;
	private MyLocationOverlay mMyLocationOverlay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mMapView = (MapView) findViewById(R.id.mapview);
		mMapView.setBuiltInZoomControls(true);
		mMapView.getController().setZoom(9);
		
		Drawable drawable = getResources().getDrawable(R.drawable.androidmarker);
		mItemizedoverlay = new HelloItemizedOverlay(drawable, this);

		GeoPoint point = new GeoPoint(10000000, 104000000);
		OverlayItem overlayitem = new OverlayItem(point, "Hello",
				"I'm in Phu Quoc");
		mItemizedoverlay.addOverlay(overlayitem);

		point = new GeoPoint(10792200, 106673600);
		overlayitem = new OverlayItem(point, "Hello", "I'm in Sai Gon");
		mItemizedoverlay.addOverlay(overlayitem);

		mMyLocationOverlay = new MyLocationOverlay(this, mMapView);
		mMyLocationOverlay.runOnFirstFix(new Runnable() {
			public void run() {
				mMapView.getController().animateTo(
						mMyLocationOverlay.getMyLocation());
			}
		});

		List<Overlay> mapOverlays = mMapView.getOverlays();
		mapOverlays.add(mItemizedoverlay);
		mapOverlays.add(mMyLocationOverlay);

		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		boolean enabled = mLocationManager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);

		if (!enabled) {
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("The GPS is not enable. Would you like to navigate to setting to enable it?");
			builder.setCancelable(true);
			builder.setPositiveButton("OK", new OkOnClickListener());
			builder.setNegativeButton("Quit", new CancelOnClickListener());
			AlertDialog dialog = builder.create();
			dialog.show();
		} 
	} 

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	protected void onPause() {
		super.onPause();

		mLocationManager.removeUpdates(this);
		mMyLocationOverlay.disableCompass();
		mMyLocationOverlay.disableMyLocation();
	}

	@Override
	protected void onResume() {
		super.onResume();

		mMyLocationOverlay.enableCompass();
		mMyLocationOverlay.enableMyLocation();
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.trace_me:
			mMapView.getController().animateTo(
					mMyLocationOverlay.getMyLocation());
			return true;
		case R.id.layer_satellite:
			mMapView.setSatellite(!mMapView.isSatellite());
			return true;
		case R.id.layer_traffic:
			mMapView.setTraffic(!mMapView.isTraffic());
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onLocationChanged(Location location) {
		// Called when a new location is found by the network location
		// provider.
//		int lat = (int) (location.getLatitude() * 1E6);
//		int lng = (int) (location.getLongitude() * 1E6);
//		GeoPoint point = new GeoPoint(lat, lng);
//
//		mMapView.getController().animateTo(point);
	}

	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(this, "Disabled provider " + provider, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onProviderEnabled(String provider) {
		Toast.makeText(this, "Enabled provider " + provider, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	private final class CancelOnClickListener implements DialogInterface.OnClickListener {
		
		public void onClick(DialogInterface dialog, int which) {
			HelloMapActivity.this.finish();
		}

	}

	private final class OkOnClickListener implements DialogInterface.OnClickListener {

		public void onClick(DialogInterface dialog, int which) {
			Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			startActivity(intent);
		}

	}

}
